//
//  TourTableViewCell.m
//  EasyToTesco2.0
//
//  Created by knew on 2017/5/3.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "TourTableViewCell.h"

@implementation TourOrderLastView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
    
        [self _initWithTourLastView];
    }
    return self;
}

- (void)_initWithTourLastView {
    self.priceLabel = [[UILabel alloc] init];
    self.priceLabel.font = [UIFont systemFontOfSize:kCellNameFontSize];
    self.priceLabel.textColor = [UIColor orangeColor];
    [self addSubview:self.priceLabel];
    
    self.payOrderButton = [[UIButton alloc] init];
    [self.payOrderButton setBackgroundColor:DEFAULT_COLOR];
    self.payOrderButton.titleLabel.font = [UIFont systemFontOfSize:kCellNameFontSize];
    [self.payOrderButton setTitle:@"立即支付" forState:UIControlStateNormal];
    self.payOrderButton.layer.cornerRadius = 5;
    self.payOrderButton.layer.masksToBounds = YES;
    [self.payOrderButton addTarget:self action:@selector(didClickLastViewPayButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.payOrderButton];
}

- (void)didClickLastViewPayButton:(UIButton *)button {
    if (self.lastViewPayButton) {
        self.lastViewPayButton();
    }
}

- (void)setTourOrderLastView:(TourOrderLastViewLayout *)lastViewLayout {
    self.priceLabel.frame = lastViewLayout.priceRect;
    
    self.payOrderButton.frame = lastViewLayout.payOrderRect;
}

@end

#pragma mark -
@implementation TourSellerOrderTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andTableViewCellConentViewStyle:(UITableViewCellConentViewStyle)UITableViewCellConentViewStyle {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.leftLabel = [[UILabel alloc] init];
        self.leftLabel.font = [UIFont systemFontOfSize:kCellNameFontSize];
        self.leftLabel.textColor = [UIColor grayColor];
        [self.contentView addSubview:self.leftLabel];
        
        switch (UITableViewCellConentViewStyle) {
            case UITableViewCellConentViewStyleDefault:
                [self _initTourSellerOrderTableViewCellConentViewStyleDefault];
                break;
                
            case UITableViewCellConentViewStyleData:
                [self _initTourSellerOrderTableViewCellConentViewStyleData];
                break;
                
            case UITableViewCellConentViewStyleCutAndAdd:
                [self _initTourSellerOrderTableViewCellConentViewStyleCutAndAdd];
                break;
                
            default:
                break;
        }
    }
    return self;
}

- (void)_initTourSellerOrderTableViewCellConentViewStyleDefault {
    
    self.inputTextField = [[UITextField alloc] init];
    [self.contentView addSubview:self.inputTextField];
}

- (void) _initTourSellerOrderTableViewCellConentViewStyleData {
    
    self.dataButton = [[UIButton alloc] init];
    [self.dataButton setTitle:@"请选择出行日期" forState:UIControlStateNormal];
    [self.dataButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.dataButton.layer.cornerRadius = 3;
    self.dataButton.layer.masksToBounds = YES;
    self.dataButton.layer.borderWidth = 1;
    self.dataButton.layer.borderColor = [UIColor grayColor].CGColor;
    [self.dataButton addTarget:self action:@selector(didClickDataButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.dataButton];
}

- (void) _initTourSellerOrderTableViewCellConentViewStyleCutAndAdd{
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setImage:[UIImage imageNamed:@"count_plus_0"] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.addButton = addBtn;
    [self.contentView addSubview:addBtn];
    
    UIButton *cutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cutBtn setImage:[UIImage imageNamed:@"count_min_0"] forState:UIControlStateNormal];
    [cutBtn addTarget:self action:@selector(cutBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.cutButton = cutBtn;
    [self.contentView addSubview:cutBtn];
    
    self.numberLabel = [[UILabel alloc]init];
    self.numberLabel.textAlignment = NSTextAlignmentCenter;
    self.numberLabel.text = @"1";
    self.numberLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.numberLabel];
}

- (void) setHeaderViewLayout:(TourSellOrderTableViewCellLayout *)SellOrderTableViewCellLayout andTableViewCellConentViewStyle:(UITableViewCellConentViewStyle)UITableViewCellConentViewStyle andLeftLabelString:(NSString *)leftString {
    
    self.leftLabel.frame = SellOrderTableViewCellLayout.leftLabelRect;
    self.leftLabel.text = leftString;
    
    switch (UITableViewCellConentViewStyle) {
        case UITableViewCellConentViewStyleDefault:
            self.inputTextField.frame = SellOrderTableViewCellLayout.textFieldRect;
            break;
            
        case UITableViewCellConentViewStyleData:
            self.dataButton.frame = SellOrderTableViewCellLayout.dataButtonRect;
            break;
            
        case UITableViewCellConentViewStyleCutAndAdd:
            self.addButton.frame = SellOrderTableViewCellLayout.addButtonRect;
            self.numberLabel.frame = SellOrderTableViewCellLayout.numberLabelRect;
            self.cutButton.frame = SellOrderTableViewCellLayout.cutButtonRect;
            break;
            
        default:
            break;
    }
}

// 数量加按钮
-(void)addBtnClick
{
    if (self.numAddBlock) {
        self.numAddBlock();
    }
}

//数量减按钮
-(void)cutBtnClick
{
    if (self.numCutBlock) {
        self.numCutBlock();
    }
}

- (void)didClickDataButton: (UIButton *)button {
    button.selected = !button.selected;
    button.tag = 10001;

    if (self.chooseDataButton) {
        self.chooseDataButton(button);
    }
}

@end

#pragma mark -
@implementation TourSellerDetailCellHeader

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self _initWithTourDetailCellHaederView];
    }
    return self;
}

- (void)_initWithTourDetailCellHaederView {
    UIView *leftView = [[UIView alloc] init];
    leftView.backgroundColor = DEFAULT_COLOR;
    leftView.layer.cornerRadius = 3;
    leftView.layer.masksToBounds = YES;
    self.leftView = leftView;
    [self addSubview:leftView];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"景点门票";
    self.titleLabel = titleLabel;
    [self addSubview:titleLabel];
}

- (void)setHeaderViewLayout:(TourSellerDetailCellHeaderLayout *)headerLayout {
    self.leftView.frame = headerLayout.leftViewRect;
    
    self.titleLabel.frame = headerLayout.titleRect;
}

@end

@implementation TourSellerDetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self _initTourSellerDetailCell];
    }
    return self;
}

- (void)_initTourSellerDetailCell {
    /*
     *titleLabel;
     *infoLabel;
     *moreLabel;
     *grayLabel;
     lineView;
     *priceLabel;
     *PayButton;
     */

    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont systemFontOfSize:kCellNameFontSize];
    [self.contentView addSubview:self.titleLabel];
    
    self.infoButton = [[UIButton alloc] init];
    [self.infoButton setTitleColor:RGB(18, 150, 219) forState:UIControlStateNormal];
    [self.infoButton setTitle:@"需提早一天23:30前订票" forState:UIControlStateNormal];
    [self.infoButton setImage:[UIImage imageNamed:@"time"] forState:UIControlStateNormal];
    self.infoButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.infoButton.imageEdgeInsets = UIEdgeInsetsMake(0, -3, 0, 5);
    self.infoButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:self.infoButton];
    
    self.moreLabel = [[UILabel alloc] init];
    NSMutableAttributedString *MoreString = [[NSMutableAttributedString alloc] initWithString:@"购买须知"];
    self.moreLabel.textColor = RGB(18, 150, 219);
    NSTextAttachment *moreattach = [[NSTextAttachment alloc] init];
    moreattach.image = [UIImage imageNamed:@"rightjiantou"];
    moreattach.bounds = CGRectMake(0, 0, 10, 10);
    NSAttributedString *String = [NSAttributedString attributedStringWithAttachment:moreattach];
    [MoreString appendAttributedString:String];
    self.moreLabel.attributedText = MoreString;
    self.moreLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:self.moreLabel];
    
    self.grayLabel = [[UILabel alloc] init];
    self.grayLabel.font = [UIFont systemFontOfSize:15];
    self.grayLabel.textAlignment = NSTextAlignmentCenter;
    self.grayLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.grayLabel];
    
    self.lineView = [[UIView alloc] init];
    self.lineView.backgroundColor = [UIColor grayColor];
    self.lineView.alpha = 0.7;
    [self.contentView addSubview:self.lineView];
    
    self.priceLabel = [[UILabel alloc] init];
    self.priceLabel.font = [UIFont systemFontOfSize:22];
    self.priceLabel.textColor = [UIColor orangeColor];
    self.priceLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.priceLabel];
    
    self.PayButton = [[UIButton alloc] init];
    self.PayButton.titleLabel.font = [UIFont systemFontOfSize:kCellNameFontSize];
    [self.PayButton setTitle:@"在线预订" forState:UIControlStateNormal];
    [self.PayButton setBackgroundColor:[UIColor orangeColor]];
    [self.PayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self.PayButton addTarget:self action:@selector(didClickDetailPayButton:) forControlEvents:UIControlEventTouchUpInside];
    self.PayButton.layer.cornerRadius = 5;
    self.PayButton.layer.masksToBounds = YES;
    [self.contentView addSubview:self.PayButton];
}

- (void)didClickDetailPayButton:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(didClickSellerDetailPayButton:)]) {
        [self.delegate didClickSellerDetailPayButton:button];
    }
}

- (void)setTourSellerDetailCellWithLayout:(TourDetailTableViewCellLayout *)cellLayout {
    /*
     *titleLabel;
     *infoLabel;
     *moreLabel;
     *grayLabel;
     lineView;
     *priceLabel;
     *PayButton;
     */
    NSLog(@"%ld-----------%@",cellLayout.tourDetailModel.title.length,cellLayout.tourDetailModel.title);
    if (cellLayout.tourDetailModel.title.length > 21) {
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        
    }
    
    
    self.titleLabel.text = cellLayout.tourDetailModel.title;
    self.grayLabel.text = [NSString stringWithFormat:@"¥ %@",cellLayout.tourDetailModel.market_price];
    self.priceLabel.text = [NSString stringWithFormat:@" ¥%@",cellLayout.tourDetailModel.yile_price];
    
    self.titleLabel.frame = cellLayout.leftTitleRect;
    [self.titleLabel sizeToFit];
    if (iPhone5) {
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        self.titleLabel.Height = 35;
    }
    self.infoButton.frame = cellLayout.infoRect;
    if (iPhone5) {
        self.infoButton.width = WIDTH * 0.7;
    }
    self.moreLabel.frame = cellLayout.moreRect;
    self.grayLabel.frame = cellLayout.garyTitleRect;
    self.lineView.frame = cellLayout.grayLineRect;
    self.priceLabel.frame = cellLayout.priceRect;
    self.PayButton.frame = cellLayout.payRect;
    if (iPhone5) {
        self.PayButton.titleLabel.font = [UIFont systemFontOfSize:14];
        self.priceLabel.font = [UIFont systemFontOfSize:18];
    }
    
}

@end

@implementation TourSellerDetailView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self _initSellerDetailView];
    }
    return self;
}

- (void)_initSellerDetailView {
    self.leftImg = [[UIImageView alloc] init];
    [self addSubview:self.leftImg];
    
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.font = [UIFont systemFontOfSize:kCellNameFontSize];
    [self addSubview:self.nameLabel];
    
    self.detailLabel = [[UILabel alloc] init];
    if(iPhone5 || iPhone6) {
        self.detailLabel.font = [UIFont systemFontOfSize:11];
    } else {
        self.detailLabel.font = [UIFont systemFontOfSize:12];
    }
    self.detailLabel.numberOfLines = 0;
    [self addSubview:self.detailLabel];
    
    self.phoneButton = [[UIButton alloc] init];
    self.phoneButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.phoneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.phoneButton setImage:[UIImage imageNamed:@"phone"] forState:UIControlStateNormal];
    [self.phoneButton addTarget:self action:@selector(didClickDetailPhoneButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.phoneButton];
    
    self.addressButton = [[UIButton alloc] init];
    self.addressButton.titleLabel.numberOfLines = 0;
    self.addressButton.titleLabel.font = [UIFont systemFontOfSize:kCellDetailFontSize];
    [self.addressButton setImage:[UIImage imageNamed:@"location"] forState:UIControlStateNormal];
    [self.addressButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self addSubview:self.addressButton];
}

- (void)didClickDetailPhoneButton:(UIButton *)button {
    if (self.phoneButtonBlock) {
        self.phoneButtonBlock();
    }
}

- (void)setDetailHeaderViewWithModel:(TourDetailHeaderLayout *)detailModel {
    /*
     iew *leftImg;
     *nameLabel;
     *detailLabel;
     *phoneButton;
     *addressLabel;
     */
    [self.leftImg sd_setImageWithURL:[NSURL URLWithString:detailModel.detailModel.sdpic] placeholderImage:[UIImage imageNamed:@"icon_NoRecord"]];
    self.leftImg.frame = detailModel.leftImgRect;
    
    self.nameLabel.text = detailModel.detailModel.title;
    self.nameLabel.frame = detailModel.titleRect;
    
    self.detailLabel.text = detailModel.detailModel.detail;
    self.detailLabel.frame = detailModel.detailRect;
    
    [self.phoneButton setTitle:detailModel.detailModel.phone forState:UIControlStateNormal];
    self.phoneButton.frame = detailModel.phoneRect;
    
    if (iPhone5) {
        self.phoneButton.titleLabel.font = [UIFont systemFontOfSize:12];
        self.addressButton.titleLabel.font = [UIFont systemFontOfSize:10];
    }
    
    [self.addressButton setTitle:detailModel.detailModel.address forState:UIControlStateNormal];
    self.addressButton.frame = detailModel.addressRect;
}

@end

@implementation TourHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self _initSearchTextField];
    }
    return self;
}

- (void)_initSearchTextField {
    self.backgroundColor = [UIColor whiteColor];
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    self.searchTextField = searchBar;
    self.searchTextField.placeholder = @"在此输入景区名称";
    self.searchTextField.searchBarStyle = UISearchBarStyleProminent;
    self.searchTextField.keyboardType = UIKeyboardAppearanceDefault;
    [self addSubview:self.searchTextField];
    
    [searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
        make.width.mas_equalTo(WIDTH);
        make.height.mas_equalTo(44);
    }];
}

@end

@implementation TourTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self _initContentCell];
    }
    
    return self;
}

- (void)_initContentCell {
    
    self.leftImg = [[UIImageView alloc] init];
    
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.font = [UIFont systemFontOfSize:kCellNameFontSize];
    self.nameLabel.backgroundColor = [UIColor whiteColor];
    
    self.detailLabel = [[UILabel alloc] init];
    self.detailLabel.font = [UIFont systemFontOfSize:kCellDetailFontSize];
    self.detailLabel.numberOfLines = 0;
    self.detailLabel.backgroundColor = [UIColor whiteColor];
    
    [self.contentView addSubview:self.leftImg];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.detailLabel];
    
}

- (void)setLayout:(TourViewLayout *)layout {

    [self.leftImg sd_setImageWithURL:[NSURL URLWithString:layout.tourModel.sdpic] placeholderImage:[UIImage imageNamed:@"icon_NoRecord"]];
    self.leftImg.frame = layout.leftRect;
    
    self.nameLabel.text = layout.tourModel.title;
    self.nameLabel.frame = layout.nameRect;
//    [self.nameLabel sizeToFit];
    
    self.detailLabel.frame = layout.detailRect;
    if (!layout.tourModel.details.length || ![layout.tourModel.details isEqualToString:@"<nil>"]) {
        self.detailLabel.text = layout.tourModel.details;
    } else {
        self.detailLabel.text = @"暂无景点详情";
    }
    
    _height = kCellHEIGHT + kCellTopMargin;
}


@end
